package com.taoyibao.hongcanting.dialog;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.event.EventGift;
import com.taoyibao.hongcanting.event.EventMenu;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.GiftMode;
import com.taoyibao.hongcanting.model.UserTableModel;
import com.taoyibao.hongcanting.ui.adapter.MyTabFragmentAdapter;
import com.taoyibao.hongcanting.ui.restaurant.area.MainBottomArea;
import com.taoyibao.hongcanting.ui.restaurant.fragment.GiftFragment;
import com.taoyibao.hongcanting.ui.restaurant.fragment.ScreenFragment;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.widget.BottomBarItem;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description: 餐厅模块地图中送礼物弹窗
 * @date :${DATA} 10:15
 */
public class DialogMapGift extends BaseFragmentDialog implements MainBottomArea.BottomSelectedListener, ViewPager.OnPageChangeListener, View.OnClickListener {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> mFragments;
    //title数据
    private List<String> mTitleData;
    private MyTabFragmentAdapter mAdapter;
    private FragmentManager mFragmentManager;
    private MainBottomArea mMainBottomArea;
    private ArrayList<GiftMode> mData;
    private RelativeLayout mLayoutMenu;
    private Animator mAnimator;//点餐按钮的动画
    private TextView mTvMenu;//桌号

    @Override
    protected void initView(View view) {
        mTabLayout = view.findViewById(R.id.tab_dialogGift_top);
        mViewPager = view.findViewById(R.id.vp_dialogGift_body);
        mViewPager.addOnPageChangeListener(this);
        mMainBottomArea = view.findViewById(R.id.layout_bottom);
        mMainBottomArea.setBottomSelectedListener(this);
        mLayoutMenu = view.findViewById(R.id.layout_menu);
        mLayoutMenu.setOnClickListener(this);
        view.findViewById(R.id.view_back).setOnClickListener(this);
        mTvMenu = view.findViewById(R.id.tv_dialog_menu);
        startAnimation();//开始点餐按钮的动画
    }

    @Override
    protected void setListener(View view) {
        super.setListener(view);

    }

    @Override
    protected int bindView() {
        return R.layout.dialog_map_gift;
    }

    @Override
    protected void loadData(Bundle bundle) {
        super.loadData(bundle);
        mData = new ArrayList<>();
        if (bundle.getParcelableArrayList(CodeConstant.GIFT_LIST) != null && bundle.getParcelableArrayList(CodeConstant.GIFT_LIST).size() > 0) {
            ArrayList<GiftMode> data = bundle.getParcelableArrayList(CodeConstant.GIFT_LIST);
            mData.addAll(data);
        }
    }

    @Override
    protected void initData() {
        mTitleData = new ArrayList<>();
        mFragments = new ArrayList<>();
        //分配数据
        for (int i = 0; i < mData.size(); i++) {
            if (TextUtils.equals(mData.get(i).name, "大屏")
                    || TextUtils.equals(mData.get(i).name, "霸屏")) {
                mFragments.add(ScreenFragment.getInstance(mData.get(i)));
            } else {
                mFragments.add(GiftFragment.getInstance(mData.get(i)));
            }
            mTitleData.add(mData.get(i).name);
        }
        mAdapter = new MyTabFragmentAdapter(getChildFragmentManager(), mTitleData, mFragments);
        //设置tab全部预加载
        mViewPager.setOffscreenPageLimit(mFragments.size());
        getTableStatus();
    }

    @Override
    protected void setData() {
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public void Selected(BottomBarItem bottomBarItem, int position) {
        dismiss();
    }

    @Override
    protected int setGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int setAnimation() {
        return 0;
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        //页面切换时取消所有选中的礼物
        EventGift eventGift = new EventGift();
        EventBus.getDefault().post(eventGift);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_menu://点餐按钮
                EventBus.getDefault().post(new EventMenu());
                dismiss();
                break;
            case R.id.view_back://顶部的空白部分
                dismiss();
                break;
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onResume() {
        super.onResume();
        mAnimator.resume();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onPause() {
        super.onPause();
        mAnimator.pause();
    }

    /**
     * 设置点餐按钮的动画
     */
    @SuppressLint("ResourceType")
    public void startAnimation() {
        mAnimator = AnimatorInflater.loadAnimator(getContext(), R.anim.anim_diancan);
        mAnimator.setTarget(mLayoutMenu);
        mAnimator.start();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        //结束动画
        if (mAnimator != null && mAnimator.isStarted()) {
            mAnimator.end();
        }
    }

    /**
     * 获取当前用户的就餐状态
     */
    public void getTableStatus() {
        JsonApiWrapper.getApiService().tableStatus(SpUtils.getToken())
                .compose(new DefaultTransformer<CompanyBaseModel<List<UserTableModel>>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<List<UserTableModel>>>(getContext(), false) {
                    @Override
                    public void onNext(CompanyBaseModel<List<UserTableModel>> result) {
                        if (result.code == NetCode.HTTP_200 && result.data != null && result.data.size() > 0) {
                            UserTableModel userTableModel = result.data.get(0);
                            if (userTableModel != null && !TextUtils.isEmpty(userTableModel.desk_code)) {
                                mTvMenu.setText("座位号" + "\n" + userTableModel.desk_code);
                            } else {
                                mTvMenu.setText(CodeUtils.getIdString(R.string.string_diancan));
                            }
                        } else {
                            super.onNext(result);
                            mTvMenu.setText(CodeUtils.getIdString(R.string.string_diancan));
                        }
                    }
                });
    }
}
